package com.dcyao.musicserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dcyao.musicserve.common.R;
import com.dcyao.musicserve.domain.Staff;
import com.dcyao.musicserve.domain.dto.StaffCollectDTO;
import com.dcyao.musicserve.domain.dto.StaffDTO;
import com.dcyao.musicserve.mapper.ConsumerMapper;
import com.dcyao.musicserve.mapper.StaffListMapper;
import com.dcyao.musicserve.mapper.StaffMapper;
import com.dcyao.musicserve.service.StaffService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @description 针对表【staff(乐谱)】的数据库操作Service实现
*/
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff>
implements StaffService{
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private ConsumerMapper consumerMapper;
    @Autowired
    private StaffListMapper staffListMapper;

    @Override
    public Page<StaffDTO> selectUpdateStaffByConsumerId(Page<StaffDTO> page, Integer consumerId) {
        return staffMapper.selectUpdateStaffByConsumerId(page, consumerId);
    }

    /**
     * 根据乐谱集id查找乐谱
     * @param page
     * @param staffListId
     * @return
     */
    @Override
    public Page<StaffDTO>  selectStaff(Page<StaffDTO> page, Integer staffListId,String name) {
        return staffMapper.selectStaff(page, staffListId,name);
    }

    /**
     * 根据乐谱集id查找乐谱不分页
     * @param staffListId
     * @return
     */
    @Override
    public R  selectStaffs(Integer staffListId) {
        List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffListId);
        return R.success("查询成功",staffDTOS);
    }
    /**
     * 分页查找
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R findPage(Integer pageNum, Integer pageSize,String name,Integer consumerId){
        LambdaQueryWrapper<Staff> wrapper = Wrappers.<Staff>lambdaQuery();
        if (StringUtils.isNotBlank(name)){
            wrapper.like(Staff::getName,name);
        }

        wrapper.eq(Staff ::getConsumerId,consumerId);
        wrapper.eq(Staff ::getType,0);

        Page<Staff> userPage = staffMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);

        return R.success("查询成功",userPage);
    }

    /**
     * 编辑
     * @param updateRequest
     * @return
     */
    @Override
    public R updateStaff(StaffDTO updateRequest) {
        Staff staff = new Staff();
        BeanUtils.copyProperties(updateRequest, staff);
        int i = staffMapper.updateById(staff);
        if (i>0) {
            return R.success("修改成功");
        } else {
            return R.error(400,"修改失败");
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R deleteStaff(Integer id) {
        if (staffMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error(400,"删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public R deleteBathIds(List<Integer> ids) {
        System.out.println("ids = " + ids);
        int i = staffMapper.deleteBatchIds(ids);
        if (i < 0) {
            return R.error(400,"批量删除失败");
        }
        return R.success("批量删除成功");
    }

    /**
     * 根据用户id查找歌曲
     * @param consumerId
     * @return
     */
   @Override
    public R selectByConsumerIds(Integer consumerId) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",consumerId);
        List<Staff> staff = staffMapper.selectList(queryWrapper);
       return R.success(null, staff);
    }

    /**
     * 根据乐谱id查找乐谱对象
     * @param staffId
     * @return
     */
    @Override
    public R selectByStaffIds(Integer staffId) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",staffId);
        List<Staff> staff = staffMapper.selectList(queryWrapper);

        return R.success(null, staff);
    }

    /**
     * 用户、乐谱集、乐谱总数
     * @return
     */
    @Override
    public List<Integer> staffCount() {
        int b = consumerMapper.consumerCount();
        int a = staffMapper.staffCount();
        int c = staffListMapper.staffListCount();
        List<Integer> list= new ArrayList();
        list.add(b);
        list.add(a);
        list.add(c);
        return list;
    }

    /**
     * 根据乐谱名模糊查询
     * @param name
     * @return
     */
    @Override
    public R likeStaffName(String name) {
        LambdaQueryWrapper<Staff> wrapper = Wrappers.<Staff>lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Staff::getName, name);
        }
        List<Staff> staff = staffMapper.selectList(wrapper);
        if (staff == null) {
            return R.error(400, "查询失败");
        }
        return R.success("查询成功",staff);
    }

    /**
     * 最新乐谱
     * @param
     * @return
     */
    @Override
    public R newStaffs() {
        LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Staff ::getCreateTime);
        queryWrapper.eq(Staff ::getType,0);


        List<Staff> staff = staffMapper.selectList(queryWrapper);
        return R.success("null",staff);
    }

    /**
     * 最热乐谱
     * @return
     */
    @Override
    public R hotStaffs() {
        LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Staff::getViewCounts);
        queryWrapper.eq(Staff ::getType,0);
        List<Staff> staff = staffMapper.selectList(queryWrapper);
        return R.success("null",staff);
    }

    /**
     * 根据用户id查询该用户收藏的乐谱详情
     * @param consumerId
     * @return
     */
    @Override
    public R likeStaff(Integer consumerId) {
        List<StaffCollectDTO> staffCollectDTOS = staffMapper.likeStaff(consumerId);
        return R.success("null",staffCollectDTOS);
    }

    /**
     * 根据用户id查询该用户收藏的乐谱详情分页
     * @param page
     * @param consumerId
     * @return
     */
    @Override
    public Page<StaffCollectDTO> likeStaffPage(Page<StaffCollectDTO> page, Integer consumerId) {

        return staffMapper.likeStaffPage(page, consumerId);
    }

    /**
     * 管理员审批
     * @param staffId
     * @return
     */
    @Override
    public R changeStaffType(Integer staffId,Integer type) {
        staffMapper.changeStaffType(staffId, type);
        return R.success("修改状态成功");
    }

    /**
     * 下载数量加一
     * @param staffId
     * @return
     */
    @Override
    public R addViewCount(Integer staffId) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",staffId);
        Staff staff = staffMapper.selectById(staffId);
        Staff staff1 =new Staff();
        staff1.setId(staffId);
        staff1.setViewCounts(staff.getViewCounts()+1);
        staffMapper.updateById(staff1);
        return R.success(null);
    }

}
